<!--
BSD LICENSE

Copyright(c) 2022 Intel Corporation. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
  * Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer.
  * Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in
    the documentation and/or other materials provided with the
    distribution.
  * Neither the name of Intel Corporation nor the names of its
    contributors may be used to endorse or promote products derived
    from this software without specific prior written permission.
    
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-->

<div class="mba-dialog">
  <div class="header-dialog">
    <h2 mat-dialog-title *ngIf="data.l3cbm">
      L3 Cache Allocation Technology (CAT)
    </h2>
    <h2 mat-dialog-title *ngIf="data.l2cbm">
      L2 Cache Allocation Technology (CAT)
    </h2>
    <h2 mat-dialog-title *ngIf="data.mba">Memory Bandwidth Allocation (MBA)</h2>
    <mat-progress-spinner
      class="loading"
      *ngIf="loading"
      color="primary"
      mode="indeterminate"
      diameter="20"
    >
    </mat-progress-spinner>
  </div>

  <span class="info-text" *ngIf="data.l3cbm">
    <p>
      L3 Cache Allocation can be configured for each pool using capacity
      bitmasks (CBMs). CBMs are used to specify the cache ways into which a
      pools application(s) can fill. The following rows represent pool CBMs and
      can be modified to enable some number of cache ways for each pool.
    </p>
    <p *ngIf="data.l3cdp">
      CDP provides separate control over code and data by enabling separate
      masks for code and data. With traditional CAT enabled, COS maps 1:1 with
      CBMs. With CDP enabled, the mapping is now 1:2 (each CLOS maps to two
      CBMs, one for code and one for data)
    </p>
    <em
      >CBM enabled bits must be contiguous and at least one bit per CBM must be
      set.
    </em>
  </span>

  <span class="info-text" *ngIf="data.mba"
    >Memory Bandwidth Allocation (MBA) provides approximate and indirect control
    over memory bandwidth available to each pool. User can provide a required
    bandwidth in either percentage format or MBps format.</span
  >

  <span class="info-text" *ngIf="data.l2cbm">
    <p>
      L2 Cache Allocation can be configured for each pool using capacity
      bitmasks (CBMs). CBMs are used to specify the cache ways into which a
      pools application(s) can fill. The following rows represent pool CBMs and
      can be modified to enable some number of cache ways for each pool.
    </p>
    <p *ngIf="data.l2cdp">
      CDP provides separate control over code and data by enabling separate
      masks for code and data. With traditional CAT enabled, COS maps 1:1 with
      CBMs. With CDP enabled, the mapping is now 1:2 (each CLOS maps to two
      CBMs, one for code and one for data)
    </p>
    <em
      >CBM enabled bits must be contiguous and at least one bit per CBM must be
      set.
    </em>
  </span>

  <mat-divider class="divider"></mat-divider>
  <mat-dialog-content class="mat-typography">
    <div class="pool" *ngFor="let pool of pools; index as poolIndex"
      [style.background]="localservice.getPoolBgColor(pool.id)"
      >
      <ul *ngIf="pool.isValid" class="error">
        <li>MBps is required!</li>
        <li>MBps value too large</li>
      </ul>
      <span
        class="pool-name"
        [ngStyle]="{ 'margin-left': (data.l3cdp || data.l2cdp) ? '3rem' : '0rem' }"
      >
        {{ pool.name }}
      </span>
      <!--L3 CAT-->
      <div class="cbm" *ngIf="data.l3cbm && !data.l3cdp">
        <div class="pool-cbm">
          <button
            disableRipple="true"
            mat-flat-button
            [ngClass]="
              data.numCacheWays > 12 ? 'cbm-button small' : 'cbm-button large'
            "
            *ngFor="let n of pool.l3Bitmask; index as bitIndex"
            (click)="onChangeL3CBM(n, poolIndex, bitIndex)"
            [style.color]="n ? 'black' : 'grey'"
            [style.borderColor]="n ? 'black' : 'grey'"
            [style.background]="n ? 'white' : 'var(--cbm-disabled-bg)'"
          >
            {{ n }}
          </button>
        </div>
        <button
          mat-flat-button
          color="primary"
          class="apply-button"
          (click)="saveL3CBM(poolIndex, pool.id)"
        >
          Apply
        </button>
      </div>
      <!--L3 CAT CDP-->
      <div class="cdp" *ngIf="data.l3cdp">
        <div class="cdp-label">
          <div class="cdp-code">Code</div>
          <div class="cdp-data">Data</div>
        </div>
        <div class="cdp-buttons">
          <div class="pool-cdp">
            <button
              mat-flat-button
              disableRipple="true"
              class="cdp-code-button"
              [ngClass]="
                data.numCacheWays > 12 ? 'cbm-button small' : 'cbm-button large'
              "
              *ngFor="let n of pool.l3BitmaskCode; index as bitIndex"
              (click)="onChangeL3CdpCode(n, poolIndex, bitIndex)"
              [style.color]="n ? 'black' : 'grey'"
              [style.borderColor]="n ? 'black' : 'grey'"
              [style.background]="n ? 'white' : 'var(--cbm-disabled-bg)'"
            >
              {{ n }}
            </button>
          </div>
          <div class="pool-cdp">
            <button
              mat-flat-button
              disableRipple="true"
              class="cdp-data-button"
              [ngClass]="
                data.numCacheWays > 12 ? 'cbm-button small' : 'cbm-button large'
              "
              *ngFor="let n of pool.l3BitmaskData; index as bitIndex"
              (click)="onChangeL3CdpData(n, poolIndex, bitIndex)"
              [style.color]="n ? 'black' : 'grey'"
              [style.borderColor]="n ? 'black' : 'grey'"
              [style.background]="n ? 'white' : 'var(--cbm-disabled-bg)'"
            >
              {{ n }}
            </button>
          </div>
          <button
            mat-flat-button
            color="primary"
            class="apply-button cdp-apply-button"
            (click)="saveL3CBM(poolIndex, pool.id)"
          >
            Apply
          </button>
        </div>
        <mat-divider class="divider"></mat-divider>
      </div>
      <!--L2 CAT-->
      <div class="cbm" *ngIf="data.l2cbm && !data.l2cdp">
        <div class="pool-cbm">
          <button
            mat-flat-button
            disableRipple="true"
            [ngClass]="
              data.numCacheWays > 12 ? 'cbm-button small' : 'cbm-button large'
            "
            *ngFor="let n of pool.l2Bitmask; index as j"
            (click)="onChangeL2CBM(n, poolIndex, j)"
            [style.color]="n ? 'black' : 'grey'"
            [style.borderColor]="n ? 'black' : 'grey'"
            [style.background]="n ? 'white' : 'var(--cbm-disabled-bg)'"
          >
            {{ n }}
          </button>
        </div>
        <button
          mat-flat-button
          color="primary"
          class="apply-button"
          (click)="saveL2CBM(poolIndex, pool.id)"
        >
          Apply
        </button>
      </div>
      <!--L2 CAT CDP-->
      <div class="cdp" *ngIf="data.l2cdp">
        <div class="cdp-label">
          <div class="cdp-code">Code</div>
          <div class="cdp-data">Data</div>
        </div>
        <div class="cdp-buttons">
          <div class="pool-cdp">
            <button
              mat-flat-button
              disableRipple="true"
              class="cdp-code-button"
              [ngClass]="
                data.numCacheWays > 12 ? 'cbm-button small' : 'cbm-button large'
              "
              *ngFor="let n of pool.l2BitmaskCode; index as bitIndex"
              (click)="onChangeL2CdpCode(n, poolIndex, bitIndex)"
              [style.color]="n ? 'black' : 'grey'"
              [style.borderColor]="n ? 'black' : 'grey'"
              [style.background]="n ? 'white' : 'var(--cbm-disabled-bg)'"
            >
              {{ n }}
            </button>
          </div>
          <div class="pool-cdp">
            <button
              mat-flat-button
              disableRipple="true"
              class="cdp-data-button"
              [ngClass]="
                data.numCacheWays > 12 ? 'cbm-button small' : 'cbm-button large'
              "
              *ngFor="let n of pool.l2BitmaskData; index as bitIndex"
              (click)="onChangeL2CdpData(n, poolIndex, bitIndex)"
              [style.color]="n ? 'black' : 'grey'"
              [style.borderColor]="n ? 'black' : 'grey'"
              [style.background]="n ? 'white' : 'var(--cbm-disabled-bg)'"
            >
              {{ n }}
            </button>
          </div>
          <button
            mat-flat-button
            color="primary"
            class="apply-button cdp-apply-button"
            (click)="saveL2CBM(poolIndex, pool.id)"
          >
            Apply
          </button>
        </div>
        <mat-divider class="divider"></mat-divider>
      </div>
      <div class="mba" *ngIf="data.mba && pool.mba">
        <div class="pool-mba">
          <mat-slider
            thumbLabel
            [value]="pool.mba"
            min="10"
            max="100"
            step="10"
            aria-label="units"
            (change)="onChangeMBA($event, poolIndex)"
          ></mat-slider>
        </div>
        <button
          mat-flat-button
          color="primary"
          class="apply-button"
          (click)="saveMBA(poolIndex, pool.id)"
        >
          Apply
        </button>
      </div>
      <div class="pool-mba-mbps" *ngIf="data.mba && pool.mba_bw">
        <mat-form-field appearance="fill" floatLabel="always">
          <mat-label>MBps</mat-label>
          <input
            matInput
            [value]="pool.mba_bw === mbaBwDefNum ? '' : pool.mba_bw"
            [placeholder]="
              pool.mba_bw === mbaBwDefNum ? 'Unrestricted' : '5000'
            "
            (input)="onChangeMbaBw($event, poolIndex)"
            required
            type="number"
          />
        </mat-form-field>
        <button
          mat-flat-button
          color="primary"
          class="apply-button"
          (click)="updateMBABW(poolIndex, pool.id)"
        >
          Apply
        </button>
        <button
          mat-flat-button
          class="reset-button"
          color="accent"
          (click)="resetMBABW(pool.id)"
        >
          Reset
        </button>
      </div>
    </div>
  </mat-dialog-content>
  <mat-dialog-actions align="end">
    <button mat-button mat-dialog-close color="primary">CLOSE</button>
  </mat-dialog-actions>
</div>
